<template>
  <NstdForm
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <NstdFormItem label="Activity name" prop="name">
      <NstdInput v-model="ruleForm.name" />
    </NstdFormItem>
    <NstdFormItem label="Activity zone" prop="region">
      <NstdSelect v-model="ruleForm.region" placeholder="Activity zone" :options="options2">
        <NstdOption label="Zone one" value="shanghai" />
        <NstdOption label="Zone two" value="beijing" />
      </NstdSelect>
    </NstdFormItem>
    <NstdFormItem label="Activity count" prop="count">
      <NstdSelectV2
        v-model="ruleForm.count"
        placeholder="Activity count"
        :options="options"
      />
    </NstdFormItem>
    <NstdFormItem label="Activity time" required>
      <el-col :span="11">
        <NstdFormItem prop="date1">
          <NstdDatePicker
            v-model="ruleForm.date1"
            type="date"
            label="Pick a date"
            placeholder="Pick a date"
            style="width: 100%"
          />
        </NstdFormItem>
      </el-col>
      <el-col class="text-center" :span="2">
        <span class="text-gray-500">-</span>
      </el-col>
      <el-col :span="11">
        <NstdFormItem prop="date2">
          <NstdTimePicker
            v-model="ruleForm.date2"
            label="Pick a time"
            placeholder="Pick a time"
            style="width: 100%"
          />
        </NstdFormItem>
      </el-col>
    </NstdFormItem>
    <NstdFormItem label="Instant delivery" prop="delivery">
      <NstdSwitch v-model="ruleForm.delivery"/>
    </NstdFormItem>
    <NstdFormItem label="Activity type" prop="type">
      <NstdCheckboxGroup v-model="ruleForm.type" :options="options1">
      </NstdCheckboxGroup>
    </NstdFormItem>
    <NstdFormItem label="Resources" prop="resource">
      <NstdRadioGrouop v-model="ruleForm.resource" :options="options3">
      </NstdRadioGrouop>
    </NstdFormItem>
    <NstdFormItem label="Activity form" prop="desc">
      <NstdInput v-model="ruleForm.desc" type="textarea" />
    </NstdFormItem>
    <NstdFormItem>
      <NstdButton type="primary" @click="submitForm(ruleFormRef)">
        Create
      </NstdButton>
      <NstdButton @click="resetForm(ruleFormRef)">Reset</NstdButton>
    </NstdFormItem>
  </NstdForm>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
const options3 = [
  {
    type: 'radio',
    label: 'Sponsor',
    value: 'Sponsor'
  },
  {
    type: 'radio',
    label: 'Venue',
    value: 'Venue'
  }
]
const options1 = [
  {
    label: 'Online activities',
    value: 'Online activities',
    type: 'name'
  },
  {
    label: 'Promotion activities',
    value: 'Promotion activities',
    type: 'name'
  },
  {
    label: 'Offline activities',
    value: 'Offline activities',
    type: 'name'
  },
  {
    label: 'Simple activities',
    value: 'Simple activities',
    type: 'name'
  }
]
const options2 = [
  {
    label: 'Zone one',
    value: 'shanghai',
  },
  {
    label: 'Zone two',
    value: 'beijing',
  }
]
interface RuleForm {
  name: string
  region: string
  count: string
  date1: string
  date2: string
  delivery: boolean
  type: string[]
  resource: string
  desc: string
}

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: 'Hello',
  region: '',
  count: '',
  date1: '',
  date2: '',
  delivery: false,
  type: [],
  resource: '',
  desc: '',
})

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  region: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change',
    },
  ],
  count: [
    {
      required: true,
      message: 'Please select Activity count',
      trigger: 'change',
    },
  ],
  date1: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change',
    },
  ],
  date2: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a time',
      trigger: 'change',
    },
  ],
  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change',
    },
  ],
  resource: [
    {
      required: true,
      message: 'Please select activity resource',
      trigger: 'change',
    },
  ],
  desc: [
    { required: true, message: 'Please input activity form', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {} else {}
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>
